<?php 
session_start();
include "dbconnect.php";
if(!isset($_SESSION['Username']))
{
  header("location:index.php");

}
else 
{
	if($_SESSION['Type']!=1) 
  header("location:unauthorized.php");
	//echo isset($_SESSION['Username']);
	
}
$costs= array();
$post=0;
$yearerr="";
$Year="";
if($_SERVER["REQUEST_METHOD"]=="POST")
{
	//$semType=$_POST['semesterType'];
		$Year=$_POST['year'];
	$sem=$_POST['term'];
	if (empty($Year))
    	{
			$yearerr = "Year is required";
		}
  		else
    	{   // $sem = clean_input($semType);
    		$Year = clean_input($Year);
				if (!preg_match("/^[0-9]{4}$/",$Year))
			   {
					  $yearerr = "Invalid Year format";
			   }
				else
				{
	
	
				
	$post=1;
	$dbh=mysql_connect($dbHost,$dbUserAndName,$dbPass);
	mysql_select_db($_DB_NAME,$dbh);
	mysql_query("SET @pyear=".$_POST["year"]);
	mysql_query("SET @psemtype='".$_POST["term"]."'");
	 $result= mysql_query("CALL  getbudgetdistribution(@pyear,@psemtype)");
	 $result1=$result2=$result;
				}
		}
}
	function clean_input($data)
	{
	  $data = trim($data);
	  $data = stripslashes($data);
	  $data = htmlspecialchars($data);
	  return $data;
	}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Budget</title>

<link href="CSS/Global.css" type="text/css" rel="stylesheet" />
<link href="CSS/Base.css" type="text/css" rel="stylesheet" />
<link href="CSS/Fixed.css" type="text/css" rel="stylesheet" />

<style type="text/css">
@import "media/css/demo_table_jui.css";
@import "media/themes/smoothness/jquery-ui-1.8.4.custom.css";
</style>

<script src="media/js/jquery.js" type="text/javascript"></script>
<script src="media/js/jquery.datatables.js" type="text/javascript"></script>
<script type="text/javascript" src="scripts/jquery-ui-1.10.3/jquery-ui-1.10.3/ui/jquery-ui.js"></script>
  
	 <script type="text/javascript">
		function populateYear()
		{
			$.ajax({
						type: "POST",
                		url: "PopulateYear.php",
                		data: {term :document.getElementById("term").value,year: document.getElementById("year").value},
					   success: function(courses){
									$("#year").autocomplete({
        								source: courses.split(',')
    								});
								 },
						error: function(){
							   	alert("Error");
							   }
					});
		}
		</script>
</head>

<body style="min-width:1280px">

<div id="branding-bar" class="gateway">
<div class="bar">
<div class="wrapper">
<h1 class="campus"><a><img  height="73" src="Resources/img/trident-tab.gif.png" width="64"/>AIMS</a></h1>
</div>
</div>
</div>
<div class="grey">
</div>
<center>
<div id="hnav" style="background:none"><!--system-region name="GLOBAL NAVIGATION"/-->

              <ul>
             <li ><a href="hireView.php">Hire</a></li>
                <li ><a href="post.php">Openings</a></li>
                <li ><a href="budget.php">Budget</a></li>
                <li ><a href="semester.php">Semester</a></li>
                <li ><a href="manage.php">Manage</a></li>
              </ul>          

</div>
</center>
		<span style="width:100px;margin-left:300px;">
                <button class="hyperbutton" onclick="location.href = 'logout.php'" >logout</button>
         </span>
         <div class="footbody">
         <table class="registrationtable">
         
         <thead>
         <tr>
              <td colspan="3">
                <label class="Header1">Budget lookup</label>
                </td>
          </tr>
          </thead>
          <form action="<?php echo basename(htmlspecialchars($_SERVER["PHP_SELF"]))?>" method="post" autocomplete="off" enctype="multipart/form-data" >
          
          <tbody>
           <tr/>
            <tr/>
			<tr/>
            <tr/>
            <tr/>
            <tr/>
            <tr/>
            <tr/>
            <tr/>
            <tr/>
            <tr/>
            <tr/><tr/>
			<tr>
                   <td>
                  <label style="color:#990000" class="left-alignmentlev1">Select a semester </label>
                  </td>
                  <td>
                       <select name="term" id="term" class="crimson-text" onchange="populateYear()">
                  
                     <option value="Fall"<?php
					  if (isset($sem) && $sem=="FALL") echo "selected='selected'";
					  else echo "";?>>Fall</option>
                   <option value="Spring"<?php
					  if (isset($sem) && $sem=="SPRING") echo "selected='selected'";
					  else echo "";?>>Spring</option>

                  </select>
                  </td>
          </tr>
          
           <tr/>
            <tr/>
			<tr/>
            <tr/>
            <tr/>
            <tr/>
            <tr/>
            <tr/>
            <tr/>
            <tr/>
            <tr/>
            
            <tr>
              <td>
              	<label style="color:#990000" class="left-alignmentlev1">Year </label>
              </td>
              <td>
               <!-- <input type="text" class="crimson-border" name="budgetyear"/>-->
                  <input type="text" id="year" name="year" class="crimson-border" onkeyup="populateYear()" value="<?php echo $Year;?>"/>
               </td>
               <td class="error">
     <?php echo $yearerr;?>
               </td>
            </tr>
            <tr/>
             <tr/>
            <tr/>
			<tr/>
            <tr/>
            <tr/>
            <tr/>
            <tr/>
            <tr/>
            <tr/>
            <tr/>
            <tr>
               <td>
              	<input type="submit" value="Lookup"></input>
              </td>
          </tr>
           <tr/>
            <tr/>
			<tr/>
            <tr/>
            <tr/>
            <tr/>
            <tr/>
            <tr/>
            <tr/>
            <tr/>
            <tr/>
          </form>
          </tbody>
          </table>
             <table class="costchart">
            <th>
            <tr>
            <td colspan="2">
            <label class="Header2">Budget Distribution</label>
            </td>
             <td colspan="3">
             <?php 
               if($post==1){
				while($row=mysql_fetch_array($result))
				{ 
				
				if(is_null($row["ugsaacost"])) $ugsaa= 0; else $ugsaa=$row["ugsaacost"];
				if(is_null($row["mssaacost"])) $mssaa= 0;else $mssaa=$row["mssaacost"];
				if(is_null($row["phdsaacost"])) $phdsaa= 0;else $phdsaa=$row["phdsaacost"];
				if(is_null($row["ughourcost"])) $ughour= 0;else $ughour=$row["ughourcost"];
				if(is_null($row["mshourcost"])) $mshour= 0;else $mshour=$row["mshourcost"];
				if(is_null($row["phdhourcost"])) $phdhour= 0;else $phdhour=$row["phdhourcost"];
				
				if(is_null($row["ugsaacount"])) $ugsaacount= 0; else $ugsaacount=$row["ugsaacount"];
				if(is_null($row["mssaacount"])) $mssaacount= 0;else $mssaacount=$row["mssaacount"];
				if(is_null($row["phdsaacount"])) $phdsaacount= 0;else $phdsaacount=$row["phdsaacount"];
				if(is_null($row["ughourcount"])) $ughourcount= 0;else $ughourcount=$row["ughourcount"];
				if(is_null($row["mshourcount"])) $mshourcount= 0;else $mshourcount=$row["mshourcount"];
				if(is_null($row["phdhourcount"])) $phdhourcount= 0;else $phdhourcount=$row["phdhourcount"];
				$budget=$row["budget"];
				}
				?>
            <label class="Header2"><?php echo $budget; ?></label>
            <?php }?>
            </td>
            
            </tr>
            <tr>
                <td style="border:0px;"> </td>
                <td>BS</td>
                <td>MS</td>
                <td>PHD</td>
                <td>Cost</td>
            </tr>
            
            </th>
            <tbody>
            	<tr>
                <td> SAA </td>
              <?php 
			     if($post==1)
				 {
						?>
                <td><label><?php echo $ugsaa;?> <label> ( <?php echo $ugsaacount;?> )</label></td>
                <td><label><?php echo $mssaa;?></label> <label> ( <?php echo $mssaacount;?> )</label></td>
                <td><label><?php echo $phdsaa;?></label> <label> ( <?php echo $phdsaacount;?> )</label></td>
                <td><label><?php $saaCost=$mssaa+$ugsaa+$phdsaa; echo $saaCost; ?></label> <label> ( <?php $saaCount=$mssaacount+$ugsaacount+$phdsaacount; echo $saaCount; ?> )</label></td>
                 <?php }?>
                </tr>
                <tr>
                <td> Hourly </td>
                <?php 
				if($post==1){
				 ?>
               	<td><label><?php  echo $ughour?></label> ( <?php  echo $ughourcount?> )</label></td>
                <td><label><?php echo $mshour?></label> ( <?php  echo $mshourcount?> )</label></td>
                <td><label><?php echo $phdhour?></label> ( <?php  echo $phdhourcount?> )</label></td>
                <td><label><?php $hourCost=$ughour+$mshour+$phdhour; echo $hourCost; ?></label>  <label> ( <?php $hourCount=$mshourcount+$ughourcount+$phdhourcount; echo $hourCount; ?> )</label></td>
                 <?php }?>
                </tr>
                <tr>
                <td> Total </td>
                 <?php 
				if($post==1){
				 ?>
                <td>
                	<label><?php  $ugcost=$ughour+$ugsaa; echo $ugcost;?></label>
                 	<label> ( <?php  $ugcount=$ughourcount+$ugsaacount; echo $ugcount;?> )</label>
                 </td>
                <td>
                <label><?php  $mscost=$mshour+$mssaa; echo $mscost;?></label>
                <label> ( <?php  $mscount=$mshourcount+$mssaacount; echo $mscount;?> )</label>
                </td>
                <td>
                <label><?php  $phdcost=$phdhour+$phdsaa; echo $phdcost;?></label>
                <label> ( <?php  $phdcount=$phdhourcount+$phdsaacount; echo $phdcount;?> )</label>
                </td>
                <td><label><?php $totalcost=$ugcost+$mscost+$phdcost;echo $totalcost;?></label>
                <label> ( <?php  $totalcount=$ugcount+$mscount+$phdcount;echo $totalcount;?> )</label>
                </td>
                 <?php }?>
                </tr>
                <tr/>
             </tbody>
            </table>
          </div>
    </body>
</html>